package com.tomcov.server;

import java.util.List;

import javax.jdo.PersistenceManager;
import javax.jdo.Query;

import com.google.gwt.user.server.rpc.RemoteServiceServlet;
import com.tomcov.client.service.UserService;
import com.tomcov.shared.UserInfo;

//http://code.google.com/intl/fr/appengine/docs/java/datastore/jdo/queries.html

public class UserServiceImpl extends RemoteServiceServlet implements UserService{

	private static final long serialVersionUID = 1L;

	public UserInfo login(String username, String pass){
		PersistenceManager pm = PMF.get().getPersistenceManager();
		Query query = pm.newQuery(com.tomcov.shared.UserInfo.class);
		query.setFilter("userName == userNameParam && password== passParam");
		query.declareParameters("String userNameParam, String passParam");
		try{
			@SuppressWarnings("unchecked")
			List<UserInfo> results = (List<UserInfo>) query.execute(username,pass);
			if(results.size()==0){
				return null;
			}
			else{UserInfo user= results.get(0);
				return  user;
			}
		}
		finally{
			query.closeAll();
			pm.close();
		}
	}

	public UserInfo addUser(UserInfo user) {
		PersistenceManager pm = PMF.get().getPersistenceManager();
		Query query= pm.newQuery(com.tomcov.shared.UserInfo.class);
		query.setFilter("userName == userNameParam");
		query.declareParameters("String userNameParam");
		try{
			@SuppressWarnings("unchecked")
			List<UserInfo> results = (List<UserInfo>) query.execute(user.getUserName());
			if(results.size()==0)	
				{pm.makePersistent(user);
				return user;}
			else{
				return null;
				}
			}
		finally{
			query.closeAll();
			pm.close();
		}
		
	}

	public UserInfo getUser(String username){
		PersistenceManager pm = PMF.get().getPersistenceManager();
		Query query = pm.newQuery(com.tomcov.shared.UserInfo.class);
		query.setFilter("userName==userNameParam");//+" && "+"pass==passParam");
		query.declareParameters("String userNameParam");//,String passParam");
		try{
			@SuppressWarnings("unchecked")
			List<UserInfo> results = (List<UserInfo>) query.execute(username);//,pass);
			if(results.size()==0){
				return null;
				}
			else{UserInfo user= results.get(0);
			return user;}
		}
		finally{
			query.closeAll();
			pm.close();
		}
	}
	
	public UserInfo updateUserInfo(UserInfo user, UserInfo modify) {
	    PersistenceManager pm = PMF.get().getPersistenceManager();
	    try {
	    	UserInfo e = pm.getObjectById(com.tomcov.shared.UserInfo.class, user.getId());
	        if (UserModify(e,modify)){
	            e.setUserName(modify.getUserName());
	            e.setFirstName(modify.getFirstName());
	            e.setLastName(modify.getLastName());
	            e.setEmailAddress(modify.getEmailAddress());
	            e.setPassword(modify.getPassword());
	            return modify;
	        } else {
	        	return null;
	            
	        }
	    } finally {
	        pm.close();
	    }
	}

	private boolean UserModify(UserInfo e, UserInfo modify) {
		
		return e.getUserName()==modify.getUserName()
		|| e.getFirstName()==modify.getFirstName()
		|| e.getLastName()==modify.getLastName()
        || e.getEmailAddress()==modify.getEmailAddress()
        || e.getPassword()==modify.getPassword();
	}
}


